package com.navinfo.platform.aero.data.rest.core.controller;

import com.navinfo.platform.aero.data.rest.core.constant.AppServiceDefine;
import com.navinfo.platform.aero.data.rest.core.dto.CommandResult;
import com.navinfo.platform.aero.data.rest.core.enumref.EnumGather;
import com.navinfo.platform.aero.data.rest.core.enumref.ResultCode;
import com.navinfo.platform.aero.data.rest.core.service.impl.QureyDataService;
import com.navinfo.platform.aero.data.rest.core.util.StringUtils;
import com.navinfo.platform.aero.data.rest.vo.ColumnsMapperCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 字段映射controller
 * @date 2018年11月23日
 * @author CaiHS
 */

@RestController
@RequestMapping(value = AppServiceDefine.API_SERVER_PREFIX + "/mapper")
public class ColumnsMapperController {

    private static final Logger logger = LoggerFactory.getLogger(ColumnsMapperController.class);
    @Autowired
    private QureyDataService service;

    /**
     * 1.2.1 查询字段映射
     * @param command 参数
     * @return 字段映射
     */
    @RequestMapping(value = "/columns",method = RequestMethod.GET)
    public CommandResult getColumns(ColumnsMapperCommand command) {
        logger.debug("查询字段映射Handler层开始" + ColumnsMapperController.class.getName(), "Method:getColumns");
        CommandResult commandResult = new CommandResult();
        try {
            commandResult = checkCommand(commandResult, command);
            if(commandResult.getResultCode() == ResultCode.CLIENT_ERROR.code()){
                return commandResult;
            } else {
                List<Map<String,Object>> data = service.queryColumns(command.getParent(), command.getChilds(), command.getCycle());
                if(data != null && data.size() > 0){
                    commandResult.setData(data);
                }
                commandResult.fillResult(ResultCode.OK);
            }
        } catch (Exception e) {
            logger.error("查询字段映射！{}", command, e);
            commandResult.setResultCode(ResultCode.SERVER_ERROR.code());
            commandResult.setMessage(ResultCode.SERVER_ERROR.result());
        }
        return commandResult;
    }
    /**
     * 入参校验
     * @param commandResult 校验结果
     * @param command 参数
     * @return 校验结果
     */
    private static CommandResult checkCommand (CommandResult commandResult, ColumnsMapperCommand command){
        logger.info(ColumnsMapperController.class.getName() + " checkCommand start");
        // parent
        if (StringUtils.isNull(command.getParent())) {
            commandResult.setResultCode(ResultCode.CLIENT_ERROR.code());
            commandResult.setMessage("必须指定父分类");
            return commandResult;
        }
        // childs
        //List<String> childs = command.getChilds();
        // cycle
        String cycle = command.getCycle();
        if(StringUtils.isNull(cycle)
                || (!EnumGather.DAY.getName().equals(cycle)
                && !EnumGather.WEEK.getName().equals(cycle)
                && !EnumGather.MONTH.getName().equals(cycle))){
            command.setCycle(EnumGather.DAY.getName());
        }
        logger.info(ColumnsMapperController.class.getName() + " checkCommand end");
        return commandResult;
    }
}

